package com.bmadmin.controller.zcgl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.domain.ZcrkDomain;
import com.bmadmin.domain.ZcwhDomain;
import com.bmadmin.service.ZcrkService;
import com.bmadmin.service.ZcwhService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/zcrk")
public class ZcrkController {
	private Logger logger = LogManager.getLogger(this.getClass().getName());
	
    @Autowired
    ZcrkService zcrkService;
    @Autowired
    ZcwhService zcwhService;
    
    /**
     * 获取资产列表
     */
	@GetMapping("/getRkList")
    public Object getRkList(HttpSession session,ZcrkDomain zcrkDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", zcrkDomain.getQs_date());
		map.put("jz_date", zcrkDomain.getJz_date());
		map.put("yxbz", zcrkDomain.getYxbz());
		logger.info(zcrkDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<ZcrkDomain> list = zcrkService.getRkList(map);
		
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    @PostMapping("/insertZcrk")
    public NormalResult insertZcrk(HttpSession session,ZcrkDomain zcrk) {
    	String log = "入库单新增>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcrk+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	zcrk.setZcrk_userid(user.getU_id());
    	zcrk.setZcrk_addtime(BMUtil.getNowTimeForString());
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcrk.getZc_bm())){
        		result.setCode("9999");
                result.setMsg("资产名称为空！");
                logger.info(log+"资产名称为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcrk.getZcrk_lx())){
        		result.setCode("9999");
                result.setMsg("入库类型为空！");
                logger.info(log+"入库类型为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcrk.getZcrk_sl())){
        		result.setCode("9999");
                result.setMsg("入库数量为空！");
                logger.info(log+"入库数量为空！");
                return result;
        	}
        	int retResult = zcrkService.insertZcrk(zcrk);
        	if(retResult>0){
        		int ret = zcrkService.AddZcSl(zcrk.getZc_bm(),zcrk.getZcrk_sl());
        		if(ret>0){
        			logger.info(log+"入库库存修改成功！");
        		}else{
        			logger.info(log+"入库库存修改失败！");
        		}
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("资产入库异常！");
            logger.error(e);
        }
        return result;
    }
    
    @PostMapping("/delZcrk")
    public NormalResult delZcrk(HttpSession session,String zcrk_id) {
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	String log = "入库单作废>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(zcrk_id)) {
            result.setCode("9999");
            result.setMsg("传入入库单ID为空!");
            return result;
        }
    	Map map = new HashMap();
		map.put("zcrk_id", zcrk_id);
        List<ZcrkDomain> list = zcrkService.getRkList(map);
        if(list.size()>0){
        	ZcrkDomain zcrk = (ZcrkDomain)list.get(0);
        	if(!zcrk.getZcrk_userid().equals(user.getU_id())){
        		result.setCode("9999");
                result.setMsg("作废失败：该入库单非您录入！");
                logger.info(log+"作废失败：该入库单非您录入！");
                return result;
        	}
        	Map map1 = new HashMap();
    		map1.put("zc_bm", zcrk.getZc_bm());
        	List<ZcwhDomain> zclist = zcwhService.getZcList(map1);
        	if(zclist==null||zclist.size()==0){
        		result.setCode("9999");
                result.setMsg("作废失败：资产登记表不存在资产编码【"+zcrk.getZc_bm()+"】的资产！");
                logger.info(log+"作废失败：资产登记表不存在资产编码【"+zcrk.getZc_bm()+"】的资产！");
                return result;
        	}
        	ZcwhDomain zcwh = zclist.get(0);
        	Double zc_sl = Double.parseDouble(zcwh.getZc_sl());
        	Double zcrk_sl = Double.parseDouble(zcrk.getZcrk_sl());
        	if(zcrk_sl>zc_sl){
        		result.setCode("9999");
                result.setMsg("作废失败：资产入库单入库数量大于资产库存数量，无法作废！");
                logger.info(log+"作废失败：资产入库单入库数量大于资产库存数量，无法作废！");
                return result;
        	}
        	int retResult = zcrkService.updateZcrkYxbz(zcrk_id);
        	if(retResult>0){
        		int ret = zcrkService.DelZcSl(zcrk.getZc_bm(),zcrk.getZcrk_sl());
        		if(ret>0){
        			logger.info(log+"入库单作废库存修改成功！");
        		}else{
        			logger.info(log+"入库单作废库存修改失败！");
        		}
        		result.setCode("0000");
                result.setMsg("作废成功！");
                logger.info(log+"作废成功！");
        	}else{
        		result.setCode("9999");
        		result.setMsg("作废失败：操作数据库失败！");
        		logger.info(log+"作废失败：操作数据库失败！");
        	}
        }else{
        	result.setCode("9999");
            result.setMsg("作废失败：未找到对应的入库单！");
            logger.info(log+"作废失败：未找到对应的入库单！");
        }
    	return result;
    }
}
